#include "lvgl/lvgl.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#include "gui_guider.h"
#include "custom.h"
#include "events_init.h"

static const char *getenv_default(const char *name, const char *dflt)
{
    return getenv(name) ?: dflt;
}

#if LV_USE_LINUX_FBDEV
static void lv_linux_disp_init(void)
{
    const char *device = getenv_default("LV_LINUX_FBDEV_DEVICE", "/dev/fb0");
    lv_display_t *disp = lv_linux_fbdev_create();

    lv_linux_fbdev_set_file(disp, device);
}
#elif LV_USE_LINUX_DRM
static void lv_linux_disp_init(void)
{
    const char *device = getenv_default("LV_LINUX_DRM_CARD", "/dev/dri/card0");
    lv_display_t *disp = lv_linux_drm_create();

    lv_linux_drm_set_file(disp, device, -1);
}
#elif LV_USE_SDL
static void lv_linux_disp_init(void)
{
    const int width = atoi(getenv("LV_SDL_VIDEO_WIDTH") ?: "800");
    const int height = atoi(getenv("LV_SDL_VIDEO_HEIGHT") ?: "480");

    lv_sdl_window_create(width, height);
}
#else
#error Unsupported configuration
#endif

// 支持中文字体样式
lv_style_t hei_font_style;
// 新主题给指定控件添加样式
static void new_theme_apply_cb(lv_theme_t *th, lv_obj_t *obj)
{
    if (lv_obj_check_type(obj, &lv_label_class) || lv_obj_check_type(obj, &lv_textarea_class))
    {
        lv_obj_add_style(obj, &hei_font_style, LV_STATE_DEFAULT);
    }
}

lv_ui guider_ui;

int main(void)
{
    lv_init();

    /*Linux display device init*/
    lv_linux_disp_init();
    printf("init input\n");
#if LV_USE_SDL
    // init input device sdl2
    lv_sdl_mouse_create();
    lv_sdl_keyboard_create();
    lv_sdl_mousewheel_create();
#elif LV_USE_LINUX_FBDEV
    // touch
    lv_indev_t *touch = lv_evdev_create(LV_INDEV_TYPE_POINTER, "/dev/input/event0");
    // 默认行为 默认与活动的(最后创建的)disp绑定，如果创建新的disp也需要将输入设备与其绑定才能在新disp上输入
    // lv_indev_set_display(touch, lv_disp_get_default());
    lv_evdev_set_calibration(touch, 0, 0, 1024, 600); // 说明输入设备的尺寸是1024,600的大小 其会根据大小自动适应显示屏大小 如果驱动和硬件层面没有设置的话就由软件层面设置
#endif
    printf("init input  end\n");
    //========================== 字体样式
    // 中文字体样式测试 使用freetype库 使用有中文字模的字库ttf 创建字体样式对象 对需要使用字体的对象设置样式style
    lv_font_t *hei_font = lv_freetype_font_create("/usr/share/fonts/simhei.ttf", LV_FREETYPE_FONT_RENDER_MODE_BITMAP, 16, LV_FREETYPE_FONT_STYLE_NORMAL);
    // lv_style_t hei_font_style;
    lv_style_init(&hei_font_style);
    lv_style_set_text_font(&hei_font_style, hei_font);
    lv_style_set_text_align(&hei_font_style, LV_TEXT_ALIGN_LEFT);

    // 设置默认主题
    // 1.获取默认主题
    lv_theme_t *default_theme = lv_theme_get_from_obj(NULL);
    // 2.设置追加的主题
    lv_theme_t append_theme = *default_theme;
    lv_theme_set_parent(&append_theme, default_theme);
    lv_theme_set_apply_cb(&append_theme, new_theme_apply_cb);
    // 3.将主题应用在显示器上
    lv_disp_set_theme(NULL, &append_theme);
    //=========================

    custom_init(&guider_ui);
    /*Handle LVGL tasks*/

    unsigned char count = 0;
    while (1)
    {
        lv_timer_handler();
        usleep(5000);

        // 切换广告  // 后续换成数组和tick调用 // 得到点击的标签切换到对应广告
        if (count == 127)
        {
            change_animing(255);
        }
        count++;
    }

    return 0;
}
